<?php
/*
Plugin Name: Access Count
Plugin URI: 
Version: v1.0
Author: <a href="http://huynhuan.wordpress.com">HuanHH</a>
Description: Power by bizonline.com.
*/
?>
<?php 
class AccessCount extends WP_Widget {

	function AccessCount() {
		$widget_ops = array('classname' => 'access_count', 'description' => 'Access Count' );
		$control_ops = array ('width' => '200', 'height' => '400');
		$this->WP_Widget('access_count', 'Access Count', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance) {
		extract($args);
		extract($instance);
		
		global $wpdb;
		$sysinfo = $wpdb->get_row("SELECT * FROM tbl_access ORDER BY LATEST_ACCESS_TIME DESC LIMIT 1");
		
		//get online
		$online = $wpdb->get_results("SELECT DISTINCT IP FROM tbl_online WHERE UNIX_TIMESTAMP(ACCESS_TIME)>".(time()-600));
		//delete old log
		$wpdb->query("DELETE FROM tbl_online WHERE UNIX_TIMESTAMP(ACCESS_TIME)<=".(time()-600));
		?>
		
		<li class="widget widget_text" id="text-3"><h2 class="widgettitle"><?php print $title; ?></h2>
			<div class="textwidget" style="width: 100%; text-align: left">
				<span>Lượt truy cập:</span>&nbsp;&nbsp;<span style="font-size: 15px; text-shadow: 0px 2px 2px #AAAAAA; letter-spacing: 3px"><?php print str_pad($sysinfo->ACCESS_COUNT, 6, 0, STR_PAD_LEFT); ?></span><br />	
				<span>Đang trực tuyến:</span>&nbsp;&nbsp;<span style="font-size: 15px; text-shadow: 0px 2px 2px #AAAAAA; letter-spacing: 3px"><?php print count($online); ?></span>
			</div>
		</li>
		<?php
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;

		$title = $new_instance['title'];
		
		$instance = compact('title');
		return $instance;
	}

	function form($instance) {
		$defaults=array(
			'title' => __('ACCESS COUNT', 'bizonline')
		);
		
		$options = wp_parse_args($instance, $defaults);
		extract($options);

		?>

		<div>
			<div>Title label:</div>
			<div><input  id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></div>
		</div>

		<?php
	}
}

if (class_exists("AccessCount")) {
	$access_count = new AccessCount();
}

function register_access_count() {
	register_widget('AccessCount');
}
add_action('widgets_init', 'register_access_count');

?>
